/*
 * Copyright (c) 2007-2020 西安交通信息投资营运有限公司 版权所有
 * 作者:张谦
 */

import React, {FC} from 'react';
import {Card, Descriptions, Drawer, Spin, Tabs} from 'antd';
import styles from '@/utils/utils.less'
import {SysUserModel} from '../data';
import HistoryLogTable from "@/components/HistoryLog";
import {isAdminUser} from "@/utils/utils";
import HaveRole from "@/pages/sys/user/components/HaveRole";
import HavePermissions from "@/pages/sys/user/components/HavePermissions";
import DeptScopePermission from "@/pages/sys/user/components/DeptScopePermission";
import LoginLogable from "@/components/LoginLog";
import {DetailsDialogProps} from "@/global";
import service from "../service";
import useLoad from "@/hooks/useLoad";
import StatusTag from "@/components/StatusTag";


const {TabPane} = Tabs;
/** 系统用户详情组件 */
const UserDetails: FC<DetailsDialogProps> = (props) => {
  const {params: {id}, visible, hide} = props;
  const {loading, data: model} = useLoad<SysUserModel>({
    isLoad: Boolean(id),
    onLoad: () => service.selectById(id)
  }, [id]);

  return (
    <Drawer
      title="查看系统用户详细信息"
      width="50%"
      visible={visible}
      onClose={hide}
      destroyOnClose
      className={styles.drawerBodyPadding}>
      <Spin spinning={loading} wrapperClassName={styles.h100p}>
        <Tabs className={styles.drawerBodyTabsPadding}>
          <TabPane tab="基本信息" key="basic">
            {model && (
              <Card bordered={false} className={styles.cardBodyDesc}>
                <Descriptions
                  column={1}
                  bordered={true}
                  size="small"
                  className={styles.detailsDescriptions}>
                  {isAdminUser() && (<Descriptions.Item label="主键">{model.id}</Descriptions.Item>)}
                  <Descriptions.Item label="账号">{model.account}</Descriptions.Item>
                  <Descriptions.Item label="姓名">{model.name}</Descriptions.Item>
                  {isAdminUser() && (<Descriptions.Item label="机构主键">{model.deptId}</Descriptions.Item>)}
                  <Descriptions.Item label="机构名称">{model.deptName}</Descriptions.Item>
                  <Descriptions.Item label="开始有效期">{model.allowStartTime}</Descriptions.Item>
                  <Descriptions.Item label="结束有效期">{model.allowEndTime}</Descriptions.Item>
                  <Descriptions.Item label="密码过期时间">{model.pwdExpireTime}</Descriptions.Item>
                  <Descriptions.Item label="登录次数">{model.loginTimes}</Descriptions.Item>
                  <Descriptions.Item label="职位">{model.post}</Descriptions.Item>
                  <Descriptions.Item label="手机">{model.mobile}</Descriptions.Item>
                  <Descriptions.Item label="邮件">{model.email}</Descriptions.Item>
                  <Descriptions.Item label="首次登录时间">{model.firstVisitTime}</Descriptions.Item>
                  <Descriptions.Item label="最后登录时间">{model.lastVisitTime}</Descriptions.Item>
                  <Descriptions.Item label="选项">
                    <StatusTag status={model.category == 1 || false} trueLable="企业账号" falseLable="系统账号"/>
                    <StatusTag status={model.pwdAllowModify == 1 || false} trueLable="允许修改密码" falseLable="不允许修改密码"/>
                    <StatusTag status={model.admin || false} trueLable="管理人员" falseLable="普通账户"/>
                    <StatusTag status={model.status || false} trueLable="启用" falseLable="禁用"/>
                    <StatusTag status={model.pwdNeverExpire == 1 || false} trueLable="密码永不过期" falseLable="密码会过期"/>
                  </Descriptions.Item>
                  <Descriptions.Item label="备注">{model.remark}</Descriptions.Item>
                </Descriptions>
              </Card>
            )}
          </TabPane>
          <TabPane tab="拥有角色" key="role">
            {model && (<HaveRole primaryKey={model.id}/>)}
          </TabPane>
          <TabPane tab="功能权限" key="modules">
            {model && (<HavePermissions primaryKey={model.id}/>)}
          </TabPane>
          <TabPane tab="机构权限" key="deptScopePermission">
            {model && (<DeptScopePermission primaryKey={model.id}/>)}
          </TabPane>
          <TabPane tab="登录日志" key="loginLog">
            {model && (<LoginLogable account={model.account}/>)}
          </TabPane>
          <TabPane tab="操作日志" key="history">
            {model && (<HistoryLogTable tableName="SysUser" primaryKey={model.id}/>)}
          </TabPane>
        </Tabs>
      </Spin>
    </Drawer>
  );
};

export default UserDetails;

